import { CopyControllerBase } from "./CopyControllerBase";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../../../Message/Command/EPublicCommand";
import { CopyConfig } from "../../../resource/CopyConfig";
import { Lv88TeamEnterWin } from "../lv88/Lv88TeamEnterWin";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { MessageBlock } from "../../../../../Framework/MQ/MessageBlock";
import { SAttribute } from "../../../../../Message/Public/SAttribute";
import { SFireMonsterCopyInfo } from "../../../../../Message/Public/SFireMonsterCopyInfo";
import { DataEvent } from "../../../events/DataEvent";
import { AIManager } from "../../../scene/ai/AIManager";
import { AIType } from "../../../scene/ai/AIType";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { TCopy } from "../../../../../Message/Db/Tables/TCopy";
import { LoaderHelp } from "../../../../common/display/LoaderHelp";
import { ResFileConst } from "../../../resource/ResFileConst";
import { Global } from "../../../../../com/gengine/global/Global";
import { ECopyType } from "../../../../../Message/Public/ECopyType";
import { Lv88SkillPanel } from "../lv88/Lv88SkillPanel";
import { Lv88CopyInfoPanel } from "../lv88/Lv88CopyInfoPanel";
import { SPublicMiniPlayer } from "../../../../../Message/Public/SPublicMiniPlayer";
import { DataProvider } from "../../../../../fl/data/DataProvider";
import { EntityUtil } from "../../../scene/layer/utils/EntityUtil";
import { Lv88LeaveWin } from "../lv88/Lv88LeaveWin";
type int = number;
//class Lv88CopyController
    
    export  class Lv88CopyController extends CopyControllerBase
    {
        constructor()
        {
            super();
            return;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.StageResize, this.stageResizeHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicFireMonsterUpdateEnergyAndRage, this.fireValueUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicFireMonsterCopyInfo, this.copyInfoUpdateHandler);
            Dispatcher.addEventListener(EventName.Lv88TeamUseProp, this.usePropHandler);
            Dispatcher.addEventListener(EventName.Lv88TeamShowEnterWin, this.showTeamEnterWinHandler);
            Dispatcher.addEventListener(EventName.AI_NpcOutDestance, this.outNpcDistanceHandler);
            return;
        }

        public showTeamEnterWin(): void
        {
            var loc1=null;
            if (this._teamEnterWin == null) 
            {
                loc1 = CopyConfig.instance.getInfoByCode(186);
                if (loc1 == null) 
                {
                    return;
                }
                this._teamEnterWin = new Lv88TeamEnterWin(loc1);
                this._teamEnterWin.addEventListener(flash.events.Event.ADDED_TO_STAGE, this.onCopyGroupAddedToStage);
                this._teamEnterWin.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onCopyGroupRemoveFromStage);
                this._teamEnterWin.addEventListener(EventName.Lv88TeamInviteReq, this.quickInviteReqHandler);
                NetDispatcher.addCmdListener(ServerCommand.Group_PlayerInfoChange, this.onGroupMemberInfoChange);
                NetDispatcher.addCmdListener(ServerCommand.Group_CaptainChange, this.onGroupMemberInfoChange);
            }
            this._teamEnterWin.show();
            return;
        }

        /* internal  */fireValueUpdateHandler(arg1: MessageBlock): void
        {
            this._fireData = arg1.messageBase as SAttribute;
            if (this._skillPanel != null) 
            {
                this._skillPanel.updateFire(this._fireData);
            }
            return;
        }

        /* internal  */copyInfoUpdateHandler(arg1: MessageBlock): void
        {
            this._copyInfo = arg1.messageBase as SFireMonsterCopyInfo;
            if (this._infoPanel == null) 
            {
                return;
            }
            this._infoPanel.updateInfo(this._copyInfo);
            return;
        }

        /* internal  */usePropHandler(arg1: DataEvent): void
        {
            var loc1=cache.scene.getNpcByNpcId(3000107);
            if (loc1 == null) 
            {
                return;
            }
            if (cache.vip.isVIP) 
            {
                AIManager.onAIControl(loc1, AIType.AI_Convey);
            }
            else 
            {
                AIManager.onAutoPathAIControl(loc1);
            }
            return;
        }

        /* internal  */showTeamEnterWinHandler(arg1: DataEvent): void
        {
            if (cache.pack.backPackCache.getItemByCode(14168001) == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(80607));
            }
            this.showTeamEnterWin();
            return;
        }

        /* internal  */outNpcDistanceHandler(arg1: DataEvent): void
        {
            if (!(this._teamEnterWin == null) && !this._teamEnterWin.isHide) 
            {
                this._teamEnterWin.hide();
            }
            return;
        }

        public /* override */ enterCopy(arg1: TCopy): void
        {
            super.enterCopy(arg1);
            LoaderHelp.addResCallBack(ResFileConst.lv88 + ".swf", this.resGot);
            if (this._teamEnterWin != null) 
            {
                this._teamEnterWin.hide();
            }
            return;
        }

        public /* override */ leaveCopy(): void
        {
            super.leaveCopy();
            if (this._skillPanel != null) 
            {
                this._skillPanel.hide();
                this._skillPanel.dispose();
                this._skillPanel = null;
            }
            if (this._infoPanel != null) 
            {
                this._infoPanel.hide();
                this._infoPanel.dispose();
                this._infoPanel = null;
            }
            return;
        }

        /* internal  */stageResizeHandler(arg1: DataEvent): void
        {
            this.replaceSkillPanel();
            return;
        }

        /* internal  */replaceSkillPanel(): void
        {
            var loc1=0;
            if (!(this._skillPanel == null) && !this._skillPanel.isHide) 
            {
                loc1 = Global.stage.stageWidth - 610;
                if (loc1 < 470) 
                {
                    loc1 = 470;
                }
                this._skillPanel.show(loc1, Global.stage.stageHeight - 55);
            }
            return;
        }

        /* internal  */resGot(): void
        {
            if (copy.copyType != ECopyType._ECopyFireMonsterIsland) 
            {
                if (copy.copyType == ECopyType._ECopyFireIslandCenter) 
                {
                    this.showCopyInfoPanel(186, Language.getString(80608), Language.getString(80609), ["2185019"]);
                }
            }
            else 
            {
                this.showSkillPanel();
                this.showCopyInfoPanel(185, Language.getString(80605), Language.getString(80602));
            }
            return;
        }

        /* internal  */showSkillPanel(): void
        {
            var loc4=null;
            var loc1=CopyConfig.instance.getMaterialList(185);
            var loc2=[];
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc4 = loc1[loc3];
                loc2.push(loc4.itemCode);
                ++loc3;
            }
            this._skillPanel = new Lv88SkillPanel(loc2.length);
            this._skillPanel.setPropListData(loc2);
            this._skillPanel.show();
            this._skillPanel.enableSkill(false);
            this.replaceSkillPanel();
            if (this._fireData != null) 
            {
                this._skillPanel.updateFire(this._fireData);
            }
            return;
        }

        /* internal  */showCopyInfoPanel(arg1: int, arg2: string, arg3: string, arg4: Array<any>=null): void
        {
            var loc1=arg4;
            if (arg4 == null) 
            {
                loc1 = Language.getString(80603).split(",");
            }
            var loc2=CopyConfig.instance.getInfoByCode(arg1);
            var loc3=[];
            if (loc2 != null) 
            {
                loc3 = loc2.reward.split(",");
            }
            this._infoPanel = new Lv88CopyInfoPanel(loc1, loc3, arg3);
            this._infoPanel.title = arg2;
            if (loc2 != null) 
            {
                this._infoPanel.leftSeconds = loc2.stayTime;
            }
            if (this._copyInfo != null) 
            {
                this._infoPanel.updateInfo(this._copyInfo);
            }
            this._infoPanel.show();
            return;
        }

        /* internal  */onCopyGroupAddedToStage(arg1: Object /* flash.events.Event */): void
        {
            this.onGroupMemberInfoChange();
            return;
        }

        /* internal  */onCopyGroupRemoveFromStage(arg1: Object /* flash.events.Event */): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.Group_PlayerInfoChange, this.onGroupMemberInfoChange);
            NetDispatcher.removeCmdListener(ServerCommand.Group_CaptainChange, this.onGroupMemberInfoChange);
            if (this._teamEnterWin == null) 
            {
                return;
            }
            this._teamEnterWin.removeEventListener(flash.events.Event.ADDED_TO_STAGE, this.onCopyGroupAddedToStage);
            this._teamEnterWin.removeEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.onCopyGroupRemoveFromStage);
            this._teamEnterWin.removeEventListener(EventName.Lv88TeamInviteReq, this.quickInviteReqHandler);
            this._teamEnterWin.dispose(false);
            this._teamEnterWin = null;
            return;
        }

        /* internal  */quickInviteReqHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc1=false;
            if (!loc1 && !cache.group.isInGroup || loc1 && !cache.crossGroup.isInGroup) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60031));
                return;
            }
            if (!loc1 && cache.copy.copyWaitingPlayers) 
            {
                loc2 = cache.copy.copyWaitingPlayers;
                loc4 = 0;
                while (loc4 < this.inviteNum) 
                {
                    if (loc2.length > loc4) 
                    {
                        loc3 = loc2[loc4]["player"] as SPublicMiniPlayer;
                        Dispatcher.dispatchEvent(new DataEvent(EventName.Group_DoInvite, loc3.entityId));
                    }
                    ++loc4;
                }
            }
            return;
        }

        /* internal  */onGroupMemberInfoChange(arg1: Object=null): void
        {
            var loc6=null;
            var loc7=false;
            var loc8=false;
            if (this._teamEnterWin == null || this._teamEnterWin.isHide) 
            {
                return;
            }
            var loc1=this._teamEnterWin.copyInfo ? this._teamEnterWin.copyInfo.copyId : 0;
            var loc2=cache.group.isCaptain;
            var loc3=cache.group.players;
            var loc4=new DataProvider();
            var loc5=0;
            while (loc5 < loc3.length) 
            {
                loc6 = loc3[loc5] as SPublicMiniPlayer;
                loc7 = EntityUtil.equal(loc6.entityId, cache.role.entityInfo.entityId);
                loc8 = cache.group.captain && EntityUtil.equal(cache.group.captain, loc6.entityId);
                loc4.addItem({"type":"", "online": loc6.online, "isCaption": loc8, "captainMode": loc2, "isMySelf": loc7, "player": loc6, "copyId": loc1, "isCross": false});
                ++loc5;
            }
            loc4.sortOn(["isCaption", "online"], Array.DESCENDING);
            this._teamEnterWin.updateGroupMemberList(loc4, loc2);
            return;
        }

        /* internal */ /* var */_skillPanel: Lv88SkillPanel;

        /* internal */ /* var */_infoPanel: Lv88CopyInfoPanel;

        /* internal */ /* var */_leaveWin: Lv88LeaveWin;

        /* internal */ /* var */_teamEnterWin: Lv88TeamEnterWin;

        /* internal */ /* var */_fireData: SAttribute;

        /* internal */ /* var */_copyInfo: SFireMonsterCopyInfo;

        /* internal */ /* var */inviteNum: int=10;
    }
